package com.enci.usbserial;

import android.util.Log;

import java.util.HashMap;
import java.util.Map;

import io.dcloud.feature.uniapp.annotation.UniJSMethod;
import io.dcloud.feature.uniapp.bridge.UniJSCallback;
import io.dcloud.feature.uniapp.common.UniModule;

/**
 * Minimal UniApp module returning a static string when invoked from JS.
 */
public class UsbSerialModule extends UniModule {

    private static final String LOG_TAG = "UsbSerialModule";
    private static final String RESPONSE_KEY_SUCCESS = "success";
    private static final String RESPONSE_KEY_MESSAGE = "message";
    private static final String HELLO_MESSAGE = "helloworld-byenci";

    @UniJSMethod(uiThread = false)
    public void hello(org.json.JSONObject options, UniJSCallback callback) {
        Log.i(LOG_TAG, "hello invoked with options: " + (options != null ? options.toString() : "null"));
        if (callback == null) {
            Log.w(LOG_TAG, "hello invoked without callback; returning early");
            return;
        }
        Map<String, Object> payload = new HashMap<>();
        payload.put(RESPONSE_KEY_SUCCESS, true);
        payload.put(RESPONSE_KEY_MESSAGE, HELLO_MESSAGE);
        Log.i(LOG_TAG, "hello payload prepared: " + payload.toString());
        Log.i(LOG_TAG, "dispatching callback result to JS layer");
        callback.invoke(payload);
        Log.i(LOG_TAG, "callback dispatched");
    }
}
